package com.anti.modular.owner.ownerleasefees.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.owner.ownerlease.param.OwerGdfy;
import com.anti.modular.owner.ownerleasefees.entity.OwnerLeaseFees;
import com.anti.modular.owner.ownerleasefees.enums.OwnerLeaseFeesExceptionEnum;
import com.anti.modular.owner.ownerleasefees.param.OwnerLeaseFeesParam;
import com.anti.modular.owner.ownerleasefees.service.OwnerLeaseFeesService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 固定費用service接口实现类
 *
 * @author anti
 * @date 2021-05-20 14:28:05
 */
@Service
public class OwnerLeaseFeesServiceImpl implements OwnerLeaseFeesService {
    @Resource
    DBUtils dbUtils;

    @Override
    public OwnerLeaseFees getById(OwnerLeaseFeesParam ownerLeaseFeesParam){
        String sql="select * from owner_lease_fees where id =? ";
        return dbUtils.get(sql,OwnerLeaseFees.class,ownerLeaseFeesParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,OwnerLeaseFeesParam ownerLeaseFeesParam) {
        StringBuilder sb=new StringBuilder("select * from owner_lease_fees where 1=1 and status =0");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(ownerLeaseFeesParam)) {
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<OwnerLeaseFees> list(OwnerLeaseFeesParam ownerLeaseFeesParam) {
        String sql="";
        return dbUtils.query(sql,OwnerLeaseFees.class);
    }

    @Override
    public List<OwnerLeaseFees> getGdfyByLeaseId(Long leaseId) {
        String sql="select * from owner_lease_fees where owner_lease_id =? and status=0";
        return dbUtils.query(sql,OwnerLeaseFees.class,leaseId);
    }

    @Override
    public List<OwerGdfy> getOwerGdfyByLeaseId(Long leaseId) {
        String sql="select a.id,a.owner_lease_id,a.fees_id as rent_amount_category,a.fees_name as rent_amount_name,a.amount from owner_lease_fees a where a.owner_lease_id =? and a.status=0";
        return dbUtils.query(sql,OwerGdfy.class,leaseId);
    }

    @Override
    public void add(OwnerLeaseFeesParam ownerLeaseFeesParam) {
        OwnerLeaseFees ownerLeaseFees = new OwnerLeaseFees();
        BeanUtil.copyProperties(ownerLeaseFeesParam, ownerLeaseFees);
        dbUtils.insert(ownerLeaseFees);
    }

    @Override
    public List<OwnerLeaseFees> addGdfyAndReturn(List<OwerGdfy> gdfy, Long pKey) {
        ArrayList<OwnerLeaseFees> arrayList = CollectionUtil.newArrayList();
        OwerGdfy owerGdfy = null;
        OwnerLeaseFees ownerLeaseFees= null;
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        Date date = new Date();
        for (int i = 0; i < gdfy.size(); i++) {
            owerGdfy = gdfy.get(i);
            ownerLeaseFees =new OwnerLeaseFees();
            ownerLeaseFees.setOwnerLeaseId(pKey);
            ownerLeaseFees.setFeesId(owerGdfy.getRentAmountCategory());
            ownerLeaseFees.setFeesName(owerGdfy.getRentAmountName());
            ownerLeaseFees.setAmount(owerGdfy.getAmount());
            ownerLeaseFees.setCreateTime(date);
            ownerLeaseFees.setStatus(0);
            ownerLeaseFees.setCreateUser(sysLoginUserId);
            String s = dbUtils.insertKey(ownerLeaseFees).toString();
            long pk = Long.parseLong(s);
            ownerLeaseFees.setId(pk);
            arrayList.add(ownerLeaseFees);
        }
        return arrayList;
    }

    public void delete(OwnerLeaseFeesParam ownerLeaseFeesParam) {
        String sql="delete  from owner_lease_fees where id =? ";
        dbUtils.execSql(sql,ownerLeaseFeesParam.getId());
    }

    @Override
    public void deleteByLeaseId(Long leaseId) {
        String sql="update owner_lease_fees set status=2,update_time=?,update_user=? where owner_lease_id =? and status=0";
        dbUtils.execSql(sql,new Date(),LoginContextHolder.me().getSysLoginUserId(),leaseId);
    }

    public void edit(OwnerLeaseFeesParam ownerLeaseFeesParam) {
        OwnerLeaseFees ownerLeaseFees = this.queryOwnerLeaseFees(ownerLeaseFeesParam);
        BeanUtil.copyProperties(ownerLeaseFeesParam, ownerLeaseFees);
        dbUtils.update(ownerLeaseFees);
    }

    @Override
    public OwnerLeaseFees detail(OwnerLeaseFeesParam ownerLeaseFeesParam) {
        return this.queryOwnerLeaseFees(ownerLeaseFeesParam);
    }

    /**
     * 获取固定費用
     *
     * @author anti
     * @date 2021-05-20 14:28:05
     */
    private OwnerLeaseFees queryOwnerLeaseFees(OwnerLeaseFeesParam ownerLeaseFeesParam) {
        OwnerLeaseFees ownerLeaseFees = this.getById(ownerLeaseFeesParam);
        if (ObjectUtil.isNull(ownerLeaseFees)) {
            throw new BusinessException(OwnerLeaseFeesExceptionEnum.NOT_EXIST);
        }
        return ownerLeaseFees;
    }


}
